﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using RuleCode.Table;

namespace chatblog
{
    public partial class bbs : System.Web.UI.Page
    {
        cbarea area;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                dealrequest();
                bind1();
                bind2();
                bind3();                
            }
            else
            {
                binduser();
            }

        }
        private void dealrequest()
        {
            
            cbuser u = cbuser.getcuruser();
            if (u.Count > 0)
            {
                Response.Redirect(ResolveUrl("~/member/default.aspx"));
                return;
            }

            area = cbarea.getcurarea();
            Literal_chatroom.Text = "总聊天室"+chatblog.zfunction.getchatroomlink("3", area.RECID.ToString());
            HyperLink_link.Text = area.cbtitle.ToString();
            this.Title = area.cbtitle.ToString();
            if (area.enableareabtn.Value == RuleCode.Enum.NoYes.No)
	        {
                Panel_area.Visible = false;	 
            }
            if (area.enableask.Value == RuleCode.Enum.NoYes.Yes)
            {
                Panel_q.Visible = true;
            }
            if (area.enablepaihang.Value == RuleCode.Enum.NoYes.Yes)
            {
                this.Panel_paihang.Visible = true;
            }
            else
            {
                this.Panel_paihang.Visible = false ;
            }
            if (area.enablegonggao.Value == RuleCode.Enum.NoYes.Yes)
            {
                this.Panel_gonggao.Visible = true;
            }
            else
            {
                this.Panel_gonggao.Visible = false;
            }
            Literal_logolink.Text = area.getlogolink();
            cbadv adv = cbadv.Find(" cbuserid = '" + area.cbuserid.ToString() + 
                "' and  cbcataid = '" +  "' ");
            if (adv.Count>0)
            {
                Literal_adv1.Text = adv.jcdesc1.ToString();
                Literal_adv2.Text = adv.jcdesc2.ToString();
                Literal_adv3.Text = adv.jcdesc3.ToString();
            }
            
        }
        /// <summary>
        /// 绑定用户信息
        /// </summary>
        private void binduser()
        {
            if (AliApi.appUserId == "")
            {
                //Label_lastvisit.Text = "你还未登录,亲登陆.";
                Label_keyongjifen.Visible = false;
                Label_zhuanjiajifen.Visible = false;
                Label1_fatieshu.Visible = false;
                Label1_huitieshu.Visible = false;
            }
            else
            {
                cbuser u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
                //Label_lastvisit.Text = "上次登录时间:" + u.jclastdate.Date.ToString();
                Label_keyongjifen.Text = "可用分:" + u.cbpoint.ToString();
                Label_zhuanjiajifen.Text = "专家分:" + u.cbpointzhuanjia.ToString();
                Label1_fatieshu.Text = "发帖数:" + u.fatieshu.ToString();
                Label1_huitieshu.Text = "回帖数:" + u.huitieshu.ToString();
                Label_keyongjifen.Visible = true;
                Label_zhuanjiajifen.Visible = true;
                Label1_fatieshu.Visible = true;
                Label1_huitieshu.Visible = true;
            }
        }

        /// <summary>
        /// 绑定基础分类
        /// </summary>
        private void bind1()
        {
            cbcata ct = cbcata.Find(" (pid1 = '" + area.cbcataid.ToString()  
                + "' and cbstatus = '" + area.cbareaid.ToString()
                + "') ", "paixu");//or ( pid1 = '0' and cbstatus = '全部' )
            DataList_all.DataSource = ct.MyTable;
            DataList_all.DataBind();

            binduser();
            DataTable a1;
            string sql = "";
            //今日发帖量
            sql = " select count(*) from vcblist where cbstatus = '有效' "
                + " and jccreatedate <= '" + DateTime.Today.AddDays(1).ToShortDateString() 
                + "' and jccreatedate >= '" + DateTime.Today.ToShortDateString() + "' "
               + " and ( cbcatastatus = '" + area.cbareaid.ToString()
                + "' or cbcatastatus = '全部')";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            Label_today.Text = a1.Rows[0].ItemArray[0].ToString();
            //昨日发帖量
            sql = " select count(*) from vcblist where cbstatus = '有效' "
                + " and jccreatedate <= '" + DateTime.Today.ToShortDateString()
                + "' and jccreatedate >= '" + DateTime.Today.AddDays(-1).ToShortDateString() + "' "
                + " and ( cbcatastatus = '" + area.cbareaid.ToString()
                + "' or cbcatastatus = '全部')";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            Label_yestoday.Text = a1.Rows[0].ItemArray[0].ToString();
            //总发帖量
            sql = " select count(*) from vcblist where cbstatus = '有效' "
                + " and ( cbcatastatus = '" + area.cbareaid.ToString()
                + "' or cbcatastatus = '全部')";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            Label_sum.Text = a1.Rows[0].ItemArray[0].ToString();
        }
        /// <summary>
        /// 最新发帖等
        /// </summary>
        private void bind2()
        {
            if (area == null)
            {
                area = cbarea.getcurarea();
            }
            if (area.enablepaihang.Value == RuleCode.Enum.NoYes.No)
            {
                return;
            }
            DataTable a1;
            string sql = "";
            sql = " select top 10 cblistid,cbtitle from vcblist where cbstatus = '有效' and cblisttype = '发帖' "
                + " and ( cbcatastatus = '" + area.cbareaid.ToString()
                + "' )";//or cbcatastatus = '全部'
            if (AliApi.appInstanceId != "")
            {
                sql += " and area = '" + AliApi.appInstanceId + "' ";
            }
            sql += " order by recid desc ";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            GridView_newnotes.DataSource = a1;
            GridView_newnotes.DataBind();
            sql = " select top 10 cblistid,cbtitle from vcblist where cbstatus = '有效' and cblisttype = '发帖' "
                + " and ( cbcatastatus = '" + area.cbareaid.ToString()
                + "' )";//or cbcatastatus = '全部'
            if (AliApi.appInstanceId != "")
            {
                sql += " and area = '" + AliApi.appInstanceId + "' ";
            }
            sql +=" and jccreatedate <= '" + DateTime.Today.AddDays(1).ToShortDateString() + "' and jccreatedate >= '" + DateTime.Today.AddDays(-30).ToShortDateString()+"' "
                +" order by (chakanshu + huifushu*10) desc ";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            GridView_mth.DataSource = a1;
            GridView_mth.DataBind();

            sql = " SELECT  top 10   count(*) as shuliang,cbuserid,max(isnull(cbusernickname,'')) as cbusernickname,max(isnull(userrecid,'')) as userrecid FROM   vcblist where cbuserid != '' and  cbstatus = '有效' "
    + " and jccreatedate >= '" + DateTime.Today.ToShortDateString() + "'  "
                + " and ( cbcatastatus = '" + area.cbareaid.ToString()
                + "' )";//or cbcatastatus = '全部'
            if (AliApi.appInstanceId != "")
            {
                sql += " and area = '" + AliApi.appInstanceId + "' ";
            }
            sql += " group by cbuserid order by shuliang desc ";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            GridView_today.DataSource = a1;
            GridView_today.DataBind();

            sql = " SELECT  top 10   count(*) as shuliang,cbuserid,max(isnull(cbusernickname,'')) as cbusernickname,max(isnull(userrecid,'')) as userrecid FROM   vcblist where cbuserid != '' and  cbstatus = '有效' "
+ " and jccreatedate >= '" + DateTime.Today.Year.ToString() + "-01-01" + "'  "
                + " and ( cbcatastatus = '" + area.cbareaid.ToString()
                + "' )";//or cbcatastatus = '全部'
            if (AliApi.appInstanceId != "")
            {
                sql += " and area = '" + AliApi.appInstanceId + "' ";
            }
            sql += " group by cbuserid order by shuliang desc ";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            GridView_year.DataSource = a1;
            GridView_year.DataBind();
        }
        /// <summary>
        /// 区域
        /// </summary>
        private void bind3()
        {
            if (AliApi.appInstanceId == "")
            {
                Label_area.Text = "当前区域:全部";
            }
            else
            {
                Label_area.Text = "当前区域:" + AliApi.appInstanceId;
            }
        }

        protected void DataList_all_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            DataTable a1;
            string sql = "";
            // 判断是否是数据项
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {

                GridView dl = e.Item.FindControl("GridView_in") as GridView;
                sql = " select * from vcbcata where  " + " pid1 = '" + ((System.Data.DataRowView)e.Item.DataItem).Row.ItemArray[3].ToString() + "' order by paixu";
                a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
                dl.DataSource = a1;
                dl.DataBind();
            }
        }

        protected void Button_all_Click(object sender, EventArgs e)
        {
            cbuser.setarea("");
            bind2();
            bind3();
        }

        protected void Button_bj_Click(object sender, EventArgs e)
        {
            cbuser.setarea("北京");
            bind2();
            bind3();
        }

        protected void Button_sh_Click(object sender, EventArgs e)
        {
            cbuser.setarea("上海");
            bind2();
            bind3();
        }

        protected void Button_gz_Click(object sender, EventArgs e)
        {
            cbuser.setarea("广东");
            bind2();
            bind3();
        }

        protected void Button_qt_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this.Button_qt, this.Button_qt.GetType(), "selectbuttonclick",
"<script type =\"text/javascript\" >selectButtonClick('" + this.Label_area.ClientID + "');</script>", false);
            bind2();
            bind3();
        }
    }
}
